<template>
  <div>
    <NavBarVue :title="title" />
    <div class="my-bind">
      <div class="form">
        <van-field v-model="phone" size="large" name="手机号" placeholder="请输入手机号" style="font-size: 16px" />
        <van-field v-model="code" type="number" maxlength="6" size="large" center clearable placeholder="请输入验证码" style="font-size: 16px">
          <template #button>
            <van-button size="small" type="primary" class="yzm" :disabled="enable_btn_getzym" :loading="loading_btn_getzym" @click="onGetYZM">{{ btn_getyzm_title }}</van-button>
          </template>
        </van-field>
        <div style="padding: 25px 16px 0">
          <van-button :loading="loading_submit" :disabled="disabled_submit" @click="onSubmit" round block type="primary" color="#05A6F0" style="font-size: 18px">绑定</van-button>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import NavBarVue from "@/components/NavBar.vue";
import { Form, Field, Button, Checkbox, CheckboxGroup, Toast } from "vant";
import { PersonGetLoginSMSCode, PersonLoginedBindPhone } from "@/util/http";
import { mixin_validate } from "@/mixin/mixins";
import Global from "@/Global";
export default {
  name: "BindPhone",
  components: {
    NavBarVue,
    [Form.name]: Form,
    [Field.name]: Field,
    [Button.name]: Button,
    [Checkbox.name]: Checkbox,
    [CheckboxGroup.name]: CheckboxGroup,
  },
  props: ["title"],
  mixins: [mixin_validate],
  // 定义属性
  data() {
    return {
      phone: "",
      code: "",
      btn_getyzm_title: "获取验证码", //获取验证码按钮文字
      enable_btn_getzym: false,
      loading_btn_getzym: false,

      loading_submit: false,
    };
  },
  computed: {
    disabled_submit() {
      if (this.phone.trim() != "" && this.code.trim() != "") return false;
      return true;
    },
  },
  // 方法集合
  methods: {
    //获取验证码
    onGetYZM() {
      if (this.phone.length === 0) {
        Toast("请输入手机号");
        return;
      }
      if (!this.validate_phone.test(this.phone)) {
        Toast("手机号格式不正确");
        return;
      }
      this.loading_btn_getzym = true;
      this.$api.get('/PersonAccount/SendPhoneCode', { mobileNum: this.phone }).then(res => {
        this.loading_btn_getzym = false;
        if (res.success) {
          Toast.success(res.msg);
          this.getYZMSuccess();
        }
      })
    },
    getYZMSuccess() {
      let count = 60;
      let timmer = setInterval(() => {
        console.log("timmer");
        this.btn_getyzm_title = `${count}s`;
        this.enable_btn_getzym = true;
        count--;
        if (count < 0) {
          this.btn_getyzm_title = "获取验证码";
          this.enable_btn_getzym = false;
          clearInterval(timmer);
        }
      }, 1000);
    },
    onSubmit() {
      if (!this.validate_phone.test(this.phone)) {
        Toast("手机号格式不正确");
        return;
      }
      this.loading_submit = true;
      this.$api.post('/PersonAccount/BindPhone', { phoneNum: this.phone, code: this.code }).then(res => {
        this.loading_submit = false;
        if (res.success) {
          Toast.success('绑定成功');
          this.onBack();
        }
        else { Toast.fail(res.msg) }
      })
    },
  },
};
</script>

<style scoped>
.my-bind {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background-color: #fff;
}

.my-bind .form {
  padding: 80px 15px 0;
}

.my-bind .yzm {
  height: 28px;
  padding: 0 10px;
  background-color: #e6f6fd;
  border: none;
  border-radius: 20px;
  font-size: 13px;
  color: #05A6F0;
  line-height: 28px;
}
</style>
